package utile;

import entity.TreeStructure;
import entity.TreeSourceNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 树状结构工具类
 * @Author: ld
 * @Date: 2021-01-28 17:55
 */
public class TreeHelper {

    private TreeHelper(){

    }
        /**
         * 获取指定树状结构中的某节点的详细数据
         * @param treeStructureList 指定树状结构列表数据
         * @param nodeId  指定节点id
         * @return 指定id的详细数据
         */
        public static  TreeStructure getNode(List<TreeStructure> treeStructureList, String nodeId) {

            for (TreeStructure obj:convertToObjList(treeStructureList)) {
                if(nodeId.equals(obj.getId())){
                    return obj;
                }
            }
            return null;
        }

    /**
     * 将树状结构转换为平铺数据结构(将含有children的树状结构转换为仅有节点和父节点的对象列表)
     * @param source 树状结构数据列表
     * @return 平铺数据结构列表
     */
    public static List<TreeStructure> convertToObjList(List<TreeStructure> source){
        List<TreeStructure> outList = new ArrayList<>();
        for (TreeStructure tTreeStructure : source) {
            if (tTreeStructure.getChildren() != null) {
                outList.add(tTreeStructure);
                convertToObjList(tTreeStructure.getChildren(), outList);
            } else {
                outList.add(tTreeStructure);
            }
        }
        return outList;
    }


        /**
         * 将树状结构转换为平铺数据结构(将含有children的树状结构转换为仅有节点和父节点的对象列表)
         * @param source 树状结构数据列表
         * @param outList 平铺数据结构列表
         */
        private static void convertToObjList(List<TreeStructure> source , List<TreeStructure> outList){
            if(outList==null){
                outList = new ArrayList<>();
            }
            for (TreeStructure tTreeStructure : source) {
                if (tTreeStructure.getChildren() != null) {
                    outList.add(tTreeStructure);
                    convertToObjList(tTreeStructure.getChildren(), outList);
                } else {
                    outList.add(tTreeStructure);
                }
            }
        }

        /**
         * 平铺数据建立成树(将仅有节点和父节点的对象列表转换为含有children的树状结构)
         * @param sourceNodeList 平铺数据列表
         * @param <T> 任意
         * @return 树状结构数据
         */
        public static <T extends TreeSourceNode> List<TreeStructure> convertToTreeList(List<T> sourceNodeList) {
            List<TreeStructure> destNodeList = new ArrayList<>();
            //第一步，找出第一级的节点
            //1.1 统计所有节点的id
            List<String> allIds = new ArrayList<String>();
            for (T sourceNode : sourceNodeList) {
                allIds.add(sourceNode.getId());
            }
            //所有父节点找不到对应的都是一级id
            for (T sourceNode : sourceNodeList) {
                if (!allIds.contains(sourceNode.getParentId())) {
                    //从每个一级节点，递归查找children
                    TreeStructure destNode = new TreeStructure();
                    destNode.setId(sourceNode.getId());
                    destNode.setName(sourceNode.getName());
                    destNode.setLevel(1);
                    destNode.setParentId(sourceNode.getParentId());
                    List<TreeStructure> myChildes = getChildren(sourceNodeList, destNode);
                    destNode.setChildren(myChildes.isEmpty() ? null : myChildes);
                    destNodeList.add(destNode);
                }
            }
            return destNodeList;
        }

        //    递归获取子节点
        private static <T extends TreeSourceNode> List<TreeStructure> getChildren(List<T> sourceNodeList, TreeStructure parentNode) {
            List<TreeStructure> childrenList = new ArrayList<>();
            for (T sourceNode : sourceNodeList) {
                if (parentNode.getId().equals(sourceNode.getParentId())) {
                    TreeStructure children = new TreeStructure();
                    children.setId(sourceNode.getId());
                    children.setName(sourceNode.getName());
                    children.setLevel(parentNode.getLevel() + 1);
                    children.setParentId(sourceNode.getParentId());
                    List<TreeStructure> myChildes = getChildren(sourceNodeList, children);
                    children.setChildren(myChildes.isEmpty() ? null : myChildes);
                    childrenList.add(children);
                }
            }
            return childrenList;
        }
    }


